/*
 * Copyright 2025 author or author.
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jcwang.commons.tools.util.net.client;

import org.springframework.http.HttpHeaders;
import org.springframework.web.socket.WebSocketSession;

/**
 * spring WebSocketSession 适配器
 *
 * @author 王金城
 **/
public class WebSocketSessionHeaderAdapter implements RequestHeaderProvider {

    private final WebSocketSession session;

    public WebSocketSessionHeaderAdapter(WebSocketSession session) {
        this.session = session;
    }

    @Override
    public String getHeader(String name) {
        return this.getHeaders().getFirst(name);
    }

    @Override
    public HttpHeaders getHeaders() {
        return session.getHandshakeHeaders();
    }

    /**
     * 创建一个适配器
     *
     * @param session WebSocketSession
     * @return {@code com.jcwang.commons.util.net.client.WebSocketSessionAdapter}
     * @author 王金城
     */
    public static WebSocketSessionHeaderAdapter of(WebSocketSession session) {
        return new WebSocketSessionHeaderAdapter(session);
    }
}
